﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business;

namespace KhoBanHang
{
    public partial class PhieuNhapKho : Form
    {
        Business.PhieuNhapKho phieuNhapKho;

        public PhieuNhapKho()
        {
            InitializeComponent();
        }

        public void LayTheoHDM(int maHDM)
        {
            phieuNhapKho = Business.PhieuNhapKho.TaoPhieuNhapKho(maHDM);
        }

        public void LayTheoPNK(int maPNK)
        {
            phieuNhapKho = Business.PhieuNhapKho.LayPhieuNhapKho(maPNK);
        }

        private void btnLuu2_Click(object sender, EventArgs e)
        {
            if (phieuNhapKho.MaPNK == 0)
            {

                CapNhatThongTinChung();
                int maPNk = Business.PhieuNhapKho.ThemMoi(phieuNhapKho);

                phieuNhapKho = Business.PhieuNhapKho.LayPhieuNhapKho(maPNk);

                LoadThongTinPhieuNhapKho();
                MessageBox.Show("Thêm mới phiếu nhập kho thành công");
            }
            else
            {
                CapNhatThongTinChung();

                Business.PhieuNhapKho.CapNhat(phieuNhapKho);
                phieuNhapKho = Business.PhieuNhapKho.LayPhieuNhapKho(phieuNhapKho.MaPNK);

                LoadThongTinPhieuNhapKho();
                MessageBox.Show("Cập nhật phiếu nhập kho thành công");
            }
        }

        private void LoadThongTinPhieuNhapKho()
        {
            if (phieuNhapKho.MaPNK != 0)
                txtMaPhieuNhap.Text = phieuNhapKho.MaPNKHienThi;
            
            txtMaHDlapphieu.Text = phieuNhapKho.MaHDNhapHienThi;
            txtMaNVLapphieu.Text = "NV" + @Session.maNV;
            txtLiDoNhap.Text = phieuNhapKho.LyDoNhap;
            txtTongGT.Text = phieuNhapKho.TongGT.ToString();
            dtpickerNgayLap.CustomFormat = "dd/MM/yyyy";

            LoadDanhSachchiTietMatHang();
        }

        private void CapNhatThongTinChung()
        {
            phieuNhapKho.MaNV = Int16.Parse(txtMaNVLapphieu.Text.Substring(2));
            phieuNhapKho.NgayNhap = dtpickerNgayLap.Value;

            phieuNhapKho.LyDoNhap = txtLiDoNhap.Text;
        }


        private void PhieuNhapKho_Load(object sender, EventArgs e)
        {
            LoadThongTinPhieuNhapKho();
            
        }

       

        private void LoadDanhSachchiTietMatHang()
        {
            BindingSource source = new BindingSource();
            source.DataSource = phieuNhapKho.DanhSachChiTietPhieuNhap;

            gridviewCTPNK.DataSource = source;
            gridviewCTPNK.RefreshEdit();
            gridviewCTPNK.Refresh();

            gridviewCTPNK.Columns["MaCTPNK"].Visible = false;
            gridviewCTPNK.Columns["MaCTPNKHienThi"].Visible = false;

            gridviewCTPNK.Columns["maPNK"].Visible = false;

            gridviewCTPNK.Columns["MaHang"].Visible = false;

            gridviewCTPNK.Columns["MaHangHienThi"].HeaderText = "Mã hàng";
           
            gridviewCTPNK.Columns["TenHang"].HeaderText = "Tên hàng";

            gridviewCTPNK.Columns["SoLuong"].HeaderText = "Số lượng";
            gridviewCTPNK.Columns["DonGiaNhap"].HeaderText = "Đơn giá nhập";
            gridviewCTPNK.Columns["HanSuDung"].HeaderText = "Hạn sử dụng";

            gridviewCTPNK.Columns["DonGiaNhap"].DefaultCellStyle.Format = "##,###,###";
            gridviewCTPNK.Columns["HanSuDung"].DefaultCellStyle.Format = "dd/MM/yyyy";
        }
    }
}
